﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SPA_Prototype.Model;
using System.Diagnostics;

namespace SPA_Prototype
{
    public enum REQUEST_TYPE { OPEN_TAB, CONFIG, BACKUP, VIP };
    public partial class Form2 : Form
    {
        public Form2(REQUEST_TYPE rt)
        {
            InitializeComponent();
            _Request_Type = rt;
        }
 
        DatabaseDataSet dataset;
        AdminManagement AdmM;
        BackupManagement BacM;
        public static VIPManagement VIPM;

        private REQUEST_TYPE _Request_Type;

        private void Form2_Load(object sender, EventArgs e)
        {
            dataset = new DatabaseDataSet();
            AdmM = new AdminManagement(dataset);
            BacM = new BackupManagement(dataset);
        }
        private void PbCancel_Click(object sender, EventArgs e)
        {
            Close();
        }
        private void PbOk_Click(object sender, EventArgs e)
        {
            Authetication();    
        }

        private void TxtPass_KeyPress(object sender, KeyPressEventArgs e)
        {
            byte[] t = BitConverter.GetBytes(e.KeyChar);
            if (t[0] == 13)
            {
                Authetication();
            }
        }
 

        // authetication 
        private void Authetication()
        {
            string user = TxtUser.Text;
            string pass = TxtPass.Text;

            USER_TYPE type = AdmM.FindUser(user, pass);

            // login fails
            if (type == USER_TYPE.NONE)
            {
                MessageBox.Show(" Wrong user or password !", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                return;
            }

            // login sucessful
          //  MessageBox.Show(" Login Successful ", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
            if (_Request_Type == REQUEST_TYPE.OPEN_TAB)
            {
                Form1.Privilege = type;     // show tab request   
                Close();
            }
            if(_Request_Type == REQUEST_TYPE.CONFIG)   // open configuration form
            {
                Close();
                new UserManagement(type,AdmM,BacM,VIPM).ShowDialog();
            }
            if (_Request_Type == REQUEST_TYPE.BACKUP)
            {
                FolderBrowserDialog browser = new FolderBrowserDialog();
                var m = browser.ShowDialog();
                if (m == System.Windows.Forms.DialogResult.OK)
                {
                    string path = Application.StartupPath;
                    string seq = DateTime.Now.Month.ToString() + "_" + DateTime.Now.Day.ToString() + "_" + DateTime.Now.Year.ToString();
                    ProcessStartInfo startInfo = new ProcessStartInfo(path + @"\WinRAR.exe");
                    startInfo.WindowStyle = ProcessWindowStyle.Maximized;
                    startInfo.Arguments = "a -afrar " + "\"" + browser.SelectedPath + @"\Database_"+ seq + ".rar\" " + "\""+path + @"\Database.sdf" + "\"";
                    /*
                     * Systax for archive : WinRAR a -afrar "path of rar file" "path of file"
                     * Notes : double quote is used incase folder's name consists of (a) space(s) " "
                     */

                    try
                    {
                        // Call WaitForExit and then the using statement will close.
                        using (Process exeProcess = Process.Start(startInfo))
                        {
                            exeProcess.WaitForExit();
                        }

                        // send email
                        string DatabasePath = browser.SelectedPath + "\\Database_" + seq + ".rar";
                        if (BacM.EmailAddr != "")
                        {
                            Email eml = new Email(BacM.EmailAddr, DatabasePath, 0);
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
                Close();
            }
 
        }    
      
    }
}
